<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>
<body>
    <script>
        var db;
        var objectStore;
        const databaseName = 'person';
        const version = '1';
        var request = window.indexedDB.open(databaseName,version);
        request.onerror = function (){}
        request.onsuccess = function(event){
            db = request.result // 拿到数据库对象
            add();
        }
        request.onupgradeneeded = function (event){
            db = event.target.result;
            if(!db.objectStoreNames.contains('person')){
                objectStore = db.createObjectStore('person',{keyPath:'id'});
            }
            objectStore.createIndex('email','email',{unique:true});
        }

        // 插入数据
        function add() {
            console.log(db)
            var request = db.transaction(['person'],'readwrite').objectStore('person').add(
                {id:1,name:'freddy',age:34,email:'freeddyhuang@qq.com'}
            )

            request.onsuccess = function () {
                console.log('数据写入成功');
            }
            request.onerror = function () {
                console.log('数据写入失败');
            }
        }
    
    </script>
</body>
</html>